package com.example.pclimb.ui.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.pclimb.R;
import com.example.pclimb.model.User;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 向导申请适配器
 */
public class GuiderApplicationAdapter extends RecyclerView.Adapter<GuiderApplicationAdapter.ApplicationViewHolder> {

    private List<User> applications = new ArrayList<>();
    private Context context;
    private OnItemClickListener onItemClickListener;

    public GuiderApplicationAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ApplicationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_guider_application, parent, false);
        return new ApplicationViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ApplicationViewHolder holder, int position) {
        User user = applications.get(position);
        
        // 设置用户名
        holder.tvUsername.setText(user.getUsername());
        
        // 设置手机号码
        String phoneNumber = user.getPhone() != null ? user.getPhone() : user.getPhoneNumber();
        if (phoneNumber != null && !phoneNumber.isEmpty()) {
            holder.tvPhone.setText(phoneNumber);
        } else {
            holder.tvPhone.setText("未设置手机号");
        }
        
        // 设置申请日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        holder.tvApplicationDate.setText(context.getString(R.string.application_date) + ": " + 
                dateFormat.format(new Date(System.currentTimeMillis())));
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(user, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return applications.size();
    }

    public void setApplications(List<User> applications) {
        this.applications = applications;
        notifyDataSetChanged();
    }

    /**
     * 点击监听接口
     */
    public interface OnItemClickListener {
        void onItemClick(User application, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    /**
     * 申请ViewHolder
     */
    static class ApplicationViewHolder extends RecyclerView.ViewHolder {
        TextView tvUsername;
        TextView tvPhone;
        TextView tvApplicationDate;

        ApplicationViewHolder(View itemView) {
            super(itemView);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            tvApplicationDate = itemView.findViewById(R.id.tv_application_date);
        }
    }
} 